using HCore.Unity.Behaviour;
using HCore.Unity.Core;
using UnityEngine;

namespace HCore.Tests.Runtime.ShootingEditor2D.Scripts.ViewController.GamePlay
{
    public class Enemy : SmartMonoBehaviour
    {
        [SerializeField] public int Speed = 3;
        private TriggerCheck2D mFall;
        private TriggerCheck2D mGround;

        private Rigidbody2D mRigidbody2D;
        private TriggerCheck2D mWall;

        private void Awake()
        {
            mRigidbody2D = Get().GetComponent<Rigidbody2D>();
            mWall = Get("WallCheck").GetComponent<TriggerCheck2D>();
            mFall = Get("FallCheck").GetComponent<TriggerCheck2D>();
            mGround = Get("GroundCheck").GetComponent<TriggerCheck2D>();
        }

        private void FixedUpdate()
        {
            // 在地板上 && 不是掉落状态 && 未触墙 ----->  向前走
            if (mGround.Triggered && mFall.Triggered && !mWall.Triggered)
            {
                mRigidbody2D.linearVelocity = new Vector2(transform.localScale.x * Speed, mRigidbody2D.linearVelocity.y);
            }
            // 不满足上面的，都应该翻转移动
            else
            {
                var transform1 = transform;
                var localScale = transform1.localScale;
                localScale.x = -localScale.x;
                transform1.localScale = localScale;
            }
        }
    }
}